package org.jivesoftware.smackx.workgroup.agent;

import java.util.Collection;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smackx.workgroup.packet.AgentInfo;
import org.jivesoftware.smackx.workgroup.packet.AgentWorkgroups;

public class Agent
{
  private Connection connection;
  private String workgroupJID;

  Agent(Connection paramConnection, String paramString)
  {
    this.connection = paramConnection;
    this.workgroupJID = paramString;
  }

  public static Collection<String> getWorkgroups(String paramString1, String paramString2, Connection paramConnection)
    throws XMPPException
  {
    AgentWorkgroups localAgentWorkgroups1 = new AgentWorkgroups(paramString2);
    localAgentWorkgroups1.setTo(paramString1);
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(localAgentWorkgroups1.getPacketID()));
    paramConnection.sendPacket(localAgentWorkgroups1);
    AgentWorkgroups localAgentWorkgroups2 = (AgentWorkgroups)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localAgentWorkgroups2 == null)
      throw new XMPPException("No response from server on status set.");
    if (localAgentWorkgroups2.getError() != null)
      throw new XMPPException(localAgentWorkgroups2.getError());
    return localAgentWorkgroups2.getWorkgroups();
  }

  public String getName()
    throws XMPPException
  {
    AgentInfo localAgentInfo1 = new AgentInfo();
    localAgentInfo1.setType(IQ.Type.GET);
    localAgentInfo1.setTo(this.workgroupJID);
    localAgentInfo1.setFrom(getUser());
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localAgentInfo1.getPacketID()));
    this.connection.sendPacket(localAgentInfo1);
    AgentInfo localAgentInfo2 = (AgentInfo)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localAgentInfo2 == null)
      throw new XMPPException("No response from server on status set.");
    if (localAgentInfo2.getError() != null)
      throw new XMPPException(localAgentInfo2.getError());
    return localAgentInfo2.getName();
  }

  public String getUser()
  {
    return this.connection.getUser();
  }

  public void setName(String paramString)
    throws XMPPException
  {
    AgentInfo localAgentInfo = new AgentInfo();
    localAgentInfo.setType(IQ.Type.SET);
    localAgentInfo.setTo(this.workgroupJID);
    localAgentInfo.setFrom(getUser());
    localAgentInfo.setName(paramString);
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localAgentInfo.getPacketID()));
    this.connection.sendPacket(localAgentInfo);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server on status set.");
    if (localIQ.getError() != null)
      throw new XMPPException(localIQ.getError());
  }
}